package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 预约业务逻辑
     * @param map
     * @return
     */
    @Override
    public Result add(Map map) throws Exception {
        //取出参数
        Integer setmealId = Integer.parseInt(map.get("setmealId").toString());
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");
        String name = (String) map.get("name");
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String orderDate = (String) map.get("orderDate");
        String orderType = (String) map.get("orderType");

        //日期格式转换
        Date string2Date = DateUtils.parseString2Date(orderDate);

        //一，检查预约日期
        //1.日期是否存在
        //根据string日期查询ordersetting

        OrderSetting orderSetting = orderSettingDao.findByDate(string2Date);
        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.日期约满
        if (orderSetting.getReservations()>=orderSetting.getNumber()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        Member member = new Member();
        //二，会员操作
        try {
            //通过手机号检查是否是会员
             member = memberDao.findByTelephone(telephone);
            if (member == null) {
                //不是会员自动注册
                member = new Member();
                member.setName(name);
                member.setSex(sex);
                member.setIdCard(idCard);
                member.setPhoneNumber(telephone);
                //注册时间
                member.setRegTime(new Date());
                memberDao.add(member);
            } else {
                //是会员检查是否重复预约
                Order order = new Order();
                order.setMemberId(member.getId());
                order.setOrderDate(string2Date);
                order.setSetmealId(setmealId);
                List<Order> orderList = orderDao.findByCondition(order);
                //重复预约情况
                if (orderList != null && orderList.size()>0) {
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }

            //进行预约
            //1.ordersetting表的reservations数据+1
            orderSetting.setReservations(orderSetting.getReservations()+1);
            orderSettingDao.update(orderSetting);
            //2.order表新增预约数据
            Integer memberId = member.getId();

            Order order = new Order();
            order.setMemberId(memberId);
            order.setOrderDate(string2Date);
            order.setOrderType(orderType);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(setmealId);
            orderDao.add(order);
            return new Result(true, MessageConstant.ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 预约成功页面所需数据
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        Map byId4Detail = orderDao.findById4Detail(id);
        return byId4Detail;
    }
}
